unit EditTournaments;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, ExtCtrls, StdCtrls, Oracle, uSQLs, DBCtrls, ComCtrls, DB,
  OracleData, uutils, gnugettext, umainutils;

type
  TuFrmEditTournaments = class(TForm)
    edt_Name: TLabeledEdit;
    edt_Buyin: TLabeledEdit;
    edt_Fee: TLabeledEdit;
    pnl_Buttons: TPanel;
    btn_Save: TSpeedButton;
    btn_Close: TSpeedButton;
    OracleQuery1: TOracleQuery;
    dtp_DateStart: TDateTimePicker;
    dtp_TimeStart: TDateTimePicker;
    Label1: TLabel;
    com_FeeType: TComboBox;
    edt_Rake: TLabeledEdit;
    com_RakeType: TComboBox;
    chb_HaveRebuys: TCheckBox;
    com_WinDistribution: TDBLookupComboBox;
    Label3: TLabel;
    OracleDataSetWin1: TOracleDataSet;
    DataSourceWin1: TDataSource;
    lbl_CurrencySymbol: TLabel;
    edt_Stack: TLabeledEdit;
    edt_Addon: TLabeledEdit;
    edt_LateRegistrationsMin: TLabeledEdit;
    Label4: TLabel;
    com_RoundsType: TDBLookupComboBox;
    lbl_RoundType: TLabel;
    DataSourceRound1: TDataSource;
    OracleDataSetRound1: TOracleDataSet;
    com_TournamentType: TDBLookupComboBox;
    Label2: TLabel;
    OracleDataSetTouName: TOracleDataSet;
    DataSourceTouType: TDataSource;
    DataSourceDisplayZone: TDataSource;
    OracleDataSetDisplayZone: TOracleDataSet;
    com_DisplayZone: TDBLookupComboBox;
    Label5: TLabel;
    procedure btn_SaveClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure btn_CloseClick(Sender: TObject);
    procedure dtp_DateStartChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure chb_HaveRebuysClick(Sender: TObject);
  private
    { Private declarations }
    m_IsClone, m_IsEdit: boolean;
    m_recordID: string;
    procedure PopulateWinDistCombo();
    procedure PopulateRoundsTypeCombo(WithRebuyRound: boolean);
    procedure PopulateTouTypesCombo();
    procedure PopulateTableZonesCombo();
  public
    procedure ShowForm(IsEdit, IsClone: boolean; recordID: string);
    { Public declarations }
  end;

var
  uFrmEditTournaments: TuFrmEditTournaments;

implementation

uses UfrmMain;
{$R *.dfm}
{ TuFrmEditTournaments }

procedure TuFrmEditTournaments.btn_CloseClick(Sender: TObject);
begin
  close();
end;

procedure TuFrmEditTournaments.btn_SaveClick(Sender: TObject);
var
  DateTimeStart, WinDistributionId, RakeType, FeeType, recordID, TournamentId: string;
begin
  if edt_Name.Text <> '' then
  begin
    TournamentId := FrmMain.stg_Tournaments.Cells[0, FrmMain.stg_Tournaments.Row];
    RakeType := IntToStr(com_RakeType.ItemIndex);
    if (RakeType <> '0') and (RakeType <> '1') then
    begin
      RakeType := '';
    end;
    FeeType := IntToStr(com_FeeType.ItemIndex);
    if (FeeType <> '0') and (FeeType <> '1') then
    begin
      FeeType := '';
    end;
    if com_WinDistribution.KeyValue <> null then
      WinDistributionId := com_WinDistribution.KeyValue
    else
      WinDistributionId := '';
    DateTimeStart := FormatDateTime('dd.mm.yyyy', dtp_DateStart.Date);
    DateTimeStart := DateTimeStart + ' ' + FormatDateTime('hh:nn:ss', dtp_TimeStart.Time);
    DateTimeStart := 'to_date(''' + DateTimeStart + ''', ''dd.mm.yyyy hh24:mi:ss'')';
    // check if is more than two tournament at same time on display checked
    { if chb_ShowOnDisplay.Checked then
      begin
      OracleQuery1.SQL.Clear;
      OracleQuery1.Variables.List.Clear;
      OracleQuery1.DeclareAndSet(':DATA', otVarchar2, ' ID<>' + m_recordID + 'AND ' + DateTimeStart);
      OracleQuery1.SQL.Text := (SQL_GetTournamentIDForDateTime);
      OracleQuery1.Execute;
      while not OracleQuery1.Eof do
      begin
      inc(RecCount);
      if RecCount > 1 then
      begin
      ShowMessage(_('For this tournament time have already assigned two tournament for display.'));
      exit;
      end;
      OracleQuery1.Next
      end;
      end; }
    if not m_IsEdit or m_IsClone then
    begin
      recordID := InsertDataIntoDB('TOURNAMENTS',
        'TOURNAMENT_TYPE_ID,LATE_REGISTRATION_MIN,NAME,BUY_IN,EVENT_START,FEE,RAKE,REBUY_ALLOWED,ADDON,WIN_DISTRIBUTION_ID,FEE_IS_PERCENT,RAKE_IS_PERCENT, DELETED, STACK, DISPLAY_TABLE_ZONE_ID',
        '''' + VarToStr(com_TournamentType.KeyValue) + ''', ''' + edt_LateRegistrationsMin.Text + ''', ''' + edt_Name.Text + ''', ''' + edt_Buyin.Text + ''', ' + DateTimeStart + ',''' +
          edt_Fee.Text + ''',''' + edt_Rake.Text + ''',''' + BToStr(chb_HaveRebuys.Checked) + ''',''' + edt_Addon.Text + ''',''' + WinDistributionId + ''',''' + FeeType + ''',''' + RakeType +
          ''',''' + '0' + ''',''' + edt_Stack.Text + ''',''' + VarToStr(com_DisplayZone.KeyValue) + '''');
      if m_IsClone and (com_RoundsType.KeyValue = null) then
      begin
        FrmMain.CloneTournamentRounds(TournamentId, recordID, false, false);
      end;
      if com_RoundsType.KeyValue <> null then
      begin
        FrmMain.CloneTournamentRounds(com_RoundsType.KeyValue, recordID, true, false);
      end;
      // ---
      // insert tournament stop
      // InsertDataIntoDB('TOURNAMENT_STOPS', 'TOURNAMENT_ID, STOP_FROM, STOP_TO', recordID + ',' + DateTimeStart + ',' + '''' + '''');
    end
    else if m_IsEdit then
    begin
      // if datetime changed then change tournament_stops
      { Query := 'SELECT * FROM TOURNAMENTS WHERE EVENT_START<>' + DateTimeStart + ' AND ID=' + m_recordID;
        OracleQuery1.SQL.Clear;
        OracleQuery1.SQL.Text := (Query);
        OracleQuery1.Execute;
        if not OracleQuery1.eof then
        begin
        // delete previus stops
        DeleteDataFromDB('TOURNAMENT_STOPS', 'TOURNAMENT_ID=' + m_recordID + ' AND (SELECT MIN(ID) FROM TOURNAMENT_STOPS WHERE TOURNAMENT_ID=' + m_recordID + ')<>ID');
        // update tournament stop
        FrmMain.UpdateDataToDB('TOURNAMENT_STOPS', 'STOP_TO=NULL,STOP_FROM=' + DateTimeStart, 'ID=(select min(id) from tournament_stops where TOURNAMENT_ID=' + m_recordID+')');
        end; }
      UpdateDataToDB('TOURNAMENTS', 'TOURNAMENT_TYPE_ID=' + '''' + VarToStr(com_TournamentType.KeyValue)
          + '''' + ',LATE_REGISTRATION_MIN=' + '''' + edt_LateRegistrationsMin.Text + '''' + ',NAME=' + '''' + edt_Name.Text + '''' + ',BUY_IN=' + '''' + edt_Buyin.Text + '''' + ',EVENT_START=' +
          DateTimeStart + ',FEE=' + '''' + edt_Fee.Text + '''' + ',RAKE=' + '''' + edt_Rake.Text + '''' + ',REBUY_ALLOWED=' + '''' + BToStr(chb_HaveRebuys.Checked)
          + '''' + ',ADDON=' + '''' + edt_Addon.Text + '''' + ',WIN_DISTRIBUTION_ID=' + '''' + WinDistributionId + '''' + ',FEE_IS_PERCENT=' + '''' + FeeType + '''' + ',RAKE_IS_PERCENT=' + '''' +
          RakeType + '''' + ',STACK=' + '''' + edt_Stack.Text + '''' + ',DISPLAY_TABLE_ZONE_ID=' + '''' + VarToStr(com_DisplayZone.KeyValue) + '''', 'ID=' + m_recordID);
      if com_RoundsType.KeyValue <> null then
      begin
        FrmMain.CloneTournamentRounds(com_RoundsType.KeyValue, m_recordID, true, false);
      end;
    end;
    FrmMain.GetGridData(FrmMain.stg_Tournaments, SQL_Tournaments, false);
    FrmMain.stg_TournamentsSelectCell(self, 0, FrmMain.stg_Tournaments.Row, CanSelect);
  end;
  close();
end;

procedure TuFrmEditTournaments.chb_HaveRebuysClick(Sender: TObject);
begin
  edt_Addon.Visible := chb_HaveRebuys.Checked;
  if not chb_HaveRebuys.Checked then
    edt_Addon.Text := '';
  PopulateRoundsTypeCombo(chb_HaveRebuys.Checked);
end;

procedure TuFrmEditTournaments.dtp_DateStartChange(Sender: TObject);
begin
  if dtp_DateStart.Date < now() then
    dtp_DateStart.Date := now();
end;

procedure TuFrmEditTournaments.FormCreate(Sender: TObject);
begin
  gnugettext.TranslateComponent(self);
end;

procedure TuFrmEditTournaments.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
    close
  else if Key = #13 then
    btn_Save.Click;
end;

procedure TuFrmEditTournaments.FormShow(Sender: TObject);
begin
  if edt_Name.Enabled then
    edt_Name.SetFocus;
end;

procedure TuFrmEditTournaments.PopulateWinDistCombo();
begin
  // ---
  OracleDataSetWin1.CloseAll;
  OracleDataSetWin1.Session := FrmMain.OracleSession1;
  OracleDataSetWin1.SQL.Clear;
  OracleDataSetWin1.SQL.Text := ('select * from WIN_DISTRIBUTION where disabled=0 union select null, null, null from WIN_DISTRIBUTION order by 2 ASC');
  // ---
  OracleDataSetWin1.Open;
  DataSourceWin1.DataSet := OracleDataSetWin1;
  com_WinDistribution.ListSource := DataSourceWin1;
  com_WinDistribution.KeyField := 'ID';
  com_WinDistribution.ListField := 'NAME';
end;

procedure TuFrmEditTournaments.PopulateTouTypesCombo();
begin
  // ---
  OracleDataSetTouName.CloseAll;
  OracleDataSetTouName.Session := FrmMain.OracleSession1;
  OracleDataSetTouName.SQL.Clear;
  OracleDataSetTouName.SQL.Text := ('select * from TOURNAMENT_TYPES union select null, null from TOURNAMENT_TYPES order by 2 ASC');
  // ---
  OracleDataSetTouName.Open;
  DataSourceTouType.DataSet := OracleDataSetTouName;
  com_TournamentType.ListSource := DataSourceTouType;
  com_TournamentType.KeyField := 'ID';
  com_TournamentType.ListField := 'NAME';
end;

procedure TuFrmEditTournaments.PopulateTableZonesCombo();
begin
  // ---
  OracleDataSetDisplayZone.CloseAll;
  OracleDataSetDisplayZone.Session := FrmMain.OracleSession1;
  OracleDataSetDisplayZone.SQL.Clear;
  OracleDataSetDisplayZone.SQL.Text := ('select id, name from TABLE_ZONES union select null, null from TABLE_ZONES where disabled=0 order by 2 ASC');
  // ---
  OracleDataSetDisplayZone.Open;
  DataSourceDisplayZone.DataSet := OracleDataSetDisplayZone;
  com_DisplayZone.ListSource := DataSourceDisplayZone;
  com_DisplayZone.KeyField := 'ID';
  com_DisplayZone.ListField := 'NAME';
  com_DisplayZone.KeyValue := null;
end;

procedure TuFrmEditTournaments.PopulateRoundsTypeCombo(WithRebuyRound: boolean);
var
  WhereCond: string;
begin
  if WithRebuyRound then
    WhereCond := 'in'
  else
    WhereCond := 'not in';
  // ---
  OracleDataSetRound1.CloseAll;
  OracleDataSetRound1.Session := FrmMain.OracleSession1;
  OracleDataSetRound1.SQL.Clear;
  OracleDataSetRound1.SQL.Text := ('select * from TOURNAMENT_ROUND_TYPES TRT' + ' where id ' + WhereCond + ' (select DISTINCT(TRT.ID) from TOURNAMENT_ROUND_TYPES TRT' +
      ' join TOURNAMENT_ROUND_TYPE_DETAILS TRD ON TRT.ID=TRD.ROUND_TYPE_ID' + ' WHERE REBUY_OR_ADDON=''REBUY'') and disabled=0' +
      ' union select null, null, null from TOURNAMENT_ROUND_TYPES order by 2 ASC');
  // ---
  OracleDataSetRound1.Open;
  DataSourceRound1.DataSet := OracleDataSetRound1;
  com_RoundsType.ListSource := DataSourceRound1;
  com_RoundsType.KeyField := 'ID';
  com_RoundsType.ListField := 'NAME';
  com_RoundsType.KeyValue := null;
end;

procedure TuFrmEditTournaments.ShowForm(IsEdit, IsClone: boolean; recordID: string);
var
  Query: string;
begin
  if (not IsClone) and (IsEdit and FrmMain.CheckSoldTicketForTournament(FrmMain.stg_Tournaments.Cells[0, FrmMain.stg_Tournaments.Row])) then
  begin
    edt_Name.Enabled := false;
    edt_Buyin.Enabled := false;
    edt_Stack.Enabled := false;
    edt_Fee.Enabled := false;
    edt_Rake.Enabled := false;
    dtp_DateStart.Enabled := false;
    dtp_TimeStart.Enabled := false;
    com_FeeType.Enabled := false;
    com_RakeType.Enabled := false;
//    com_WinDistribution.Enabled := false;
    com_RoundsType.Enabled := false;
    com_TournamentType.Enabled := false;
  end
  else
  begin
    dtp_DateStart.Enabled := true;
    dtp_TimeStart.Enabled := true;
    edt_Name.Enabled := true;
    edt_Buyin.Enabled := true;
    edt_Stack.Enabled := true;
    edt_LateRegistrationsMin.Enabled := true;
    edt_Fee.Enabled := true;
    edt_Rake.Enabled := true;
    com_FeeType.Enabled := true;
    com_RakeType.Enabled := true;
//    com_WinDistribution.Enabled := true;
    com_RoundsType.Enabled := true;
    com_TournamentType.Enabled := true;
  end;
  m_IsClone := IsClone;
  m_IsEdit := IsEdit;
  m_recordID := recordID;
  lbl_CurrencySymbol.Caption := Currency_Symbol;
  OracleQuery1.Session := FrmMain.OracleSession1;
  edt_Name.Text := '';
  edt_Buyin.Text := '';
  dtp_TimeStart.Time := 0;
  dtp_DateStart.DateTime := now();
  edt_Fee.Text := '';
  com_FeeType.Items.Clear;
  com_FeeType.Items.Add(Currency_Symbol);
  com_FeeType.Items.Add('%');
  com_FeeType.Items.Add('');
  com_FeeType.ItemIndex := 0;
  edt_Rake.Text := '';
  edt_Addon.Text := '';
  edt_Stack.Text := '';
  com_RakeType.Items.Clear;
  com_RakeType.Items.Add(Currency_Symbol);
  com_RakeType.Items.Add('%');
  com_RakeType.Items.Add('');
  com_RakeType.ItemIndex := 1;
  chb_HaveRebuys.Checked := false;
  edt_LateRegistrationsMin.Text := '';
  PopulateWinDistCombo();
  PopulateTouTypesCombo();
  PopulateTableZonesCombo();
  com_WinDistribution.KeyValue := null;
  if IsClone then
  begin
    self.Caption := _('CLONE TOURNAMENT');
    lbl_RoundType.Visible := false;
    com_RoundsType.Visible := false;
    com_RoundsType.KeyValue := null;
  end
  else
  begin
    self.Caption := _('TOURNAMENT');
    PopulateRoundsTypeCombo(chb_HaveRebuys.Checked);
    lbl_RoundType.Visible := true;
    com_RoundsType.Visible := true;
  end;
  if IsEdit then
  begin
    Query := 'select * from TOURNAMENTS where id=' + recordID;
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := (Query);
    OracleQuery1.Execute;
    edt_Name.Text := OracleQuery1.Field('NAME');
    edt_Buyin.Text := OracleQuery1.Field('BUY_IN');
    dtp_TimeStart.Time := StrToDateTime(OracleQuery1.Field('EVENT_START'));
    dtp_DateStart.Date := StrToDateTime(OracleQuery1.Field('EVENT_START'));
    edt_Fee.Text := OracleQuery1.Field('FEE');
    edt_Stack.Text := OracleQuery1.Field('STACK');
    com_DisplayZone.KeyValue := OracleQuery1.Field('DISPLAY_TABLE_ZONE_ID');
    com_FeeType.ItemIndex := OracleQuery1.Field('FEE_IS_PERCENT');
    com_RakeType.ItemIndex := OracleQuery1.Field('RAKE_IS_PERCENT');
    edt_Rake.Text := OracleQuery1.Field('RAKE');
    chb_HaveRebuys.Checked := StrToB(OracleQuery1.Field('REBUY_ALLOWED'));
    edt_Addon.Text := OracleQuery1.Field('ADDON');
    com_WinDistribution.KeyValue := OracleQuery1.Field('WIN_DISTRIBUTION_ID');
    com_TournamentType.KeyValue := OracleQuery1.Field('TOURNAMENT_TYPE_ID');
    edt_LateRegistrationsMin.Text := OracleQuery1.Field('LATE_REGISTRATION_MIN');
  end;
  ShowModal();
end;

end.
